package view_manage

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
	"vrcm/src/vrcm/app/admin/models/view"
	"vrcm/src/vrcm/app/admin/service/dto/view_manage"
	"vrcm/src/vrcm/app/admin/service/view_manage"
	"vrcm/src/vrcm/common/apis"
)

type ViewCheck struct {
	api.Api
}

// GetPage 获取ViewCheck列表
// @Summary 获取ViewCheck列表
// @Description 获取ViewCheck列表
// @Tags ViewCheck
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.ViewCheck}} "{"code": 200, "data": [...]}"
// @Router /api/v1/view-check [get]
// @Security Bearer
func (e ViewCheck) GetPage(c *gin.Context) {
	req := view_manage.ViewCheckGetPageReq{}
	s := service.ViewCheck{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	list := make([]view.ViewCheck, 0)
	var count int64

	err = s.GetPage(&req, &list, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取ViewCheck失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Insert 创建ViewCheck
// @Summary 创建ViewCheck
// @Description 创建ViewCheck
// @Tags ViewCheck
// @Accept application/json
// @Product application/json
// @Param data body dto.ViewCheckInsertReq true "data"
// @Success 200 {object} response.Response	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/view-check [post]
// @Security Bearer
func (e ViewCheck) Insert(c *gin.Context) {
	req := view_manage.ViewCheckInsertReq{}
	s := service.ViewCheck{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.Insert(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("创建ViewCheck失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(req.GetId(), "创建成功")
}

// Update 修改ViewCheck
// @Summary 修改ViewCheck
// @Description 修改ViewCheck
// @Tags ViewCheck
// @Accept application/json
// @Product application/json
// @Param id path int true "id"
// @Param data body dto.ViewCheckUpdateReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/view-check/{id} [put]
// @Security Bearer
func (e ViewCheck) Update(c *gin.Context) {
	req := view_manage.ViewCheckUpdateReq{}
	s := service.ViewCheck{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.Update(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("修改ViewCheck失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "修改成功")
}

// Delete 删除ViewCheck
// @Summary 删除ViewCheck
// @Description 删除ViewCheck
// @Tags ViewCheck
// @Param data body dto.ViewCheckDeleteReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/view-check [delete]
// @Security Bearer
func (e ViewCheck) Delete(c *gin.Context) {
	s := service.ViewCheck{}
	req := view_manage.ViewCheckDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.Remove(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除ViewCheck失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "删除成功")
}

func (e ViewCheck) ResetViewCode(c *gin.Context) {
	req := view_manage.ResetViewCodeReq{}
	s := service.ViewCheck{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.ResetViewCode(&req)
	if err != nil {
		e.Error(apis.ResetViewCodeError.Code, err, fmt.Sprintf("重制ViewCheck失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(nil, "重制成功")
}

func (e ViewCheck) UpdateJoinTime(c *gin.Context) {
	req := view_manage.UpdateJoinTimeReq{}
	s := service.ViewCheck{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.UpdateJoinTime(&req)
	if err != nil {
		e.Error(apis.BindAccountError.Code, err, fmt.Sprintf("绑定ViewCheck失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(nil, "绑定账号或者更新假如时间成功")
}

func (e ViewCheck) GetViewCodeDetail(c *gin.Context) {
	req := view_manage.GetViewCodeDetailReq{}
	s := service.ViewCheck{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	data, err := s.GetViewCodeDetail(&req)
	if err != nil {
		e.Error(apis.GetViewCodeDetailError.Code, err, fmt.Sprintf("GetViewCodeDetail失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(data, "获取观看码使用情况成功")
}
